<template>
  <div class="info-dialog">
    <el-dialog
      :visible.sync="visible"
      :title="title"
      :modal-append-to-body="true"
      :append-to-body="true"
      :destroy-on-close="false"
      :modal="true"
      :close-on-click-modal="false"
      top="10vh"
      width="650px"
      @closed="closed"
    >
          <el-form ref="relayForm"  :model="relayForm" label-width="110px" :rules="rules" style="padding: 20px;">

            <el-form-item label="ip" prop="ip">
              <el-input v-model="relayForm.ip" placeholder="请输入IP" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="stunPort" prop="stunPort">
              <el-input v-model="relayForm.stunPort" placeholder="请输入Port" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="turnPort" prop="turnPort">
              <el-input v-model="relayForm.turnPort" placeholder="请输入Port" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="username" prop="username">
              <el-input v-model="relayForm.username" placeholder="请输入账号" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="password" prop="password">
              <el-input v-model="relayForm.password" placeholder="请输入密码" autocomplete="off" show-password></el-input>
            </el-form-item>

            <el-form-item label="启用">
              <el-switch
                v-model="relayForm.enabled"
                active-value="1"
                inactive-value="0"
                >
              </el-switch>
            </el-form-item>

          </el-form>
      <div style="text-align: right;" slot="footer" class="dialog-footer">
        <el-button @click="closed" :loading="loading">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    saveServer,
    getServerById
  } from '@/api/cloud/server'
  import {
    isOk
  } from '@/utils/request'
  export default {
    name: 'RelayDialog',
    components: {
    },
    props: {
      properties: {
        type: Object,
        default: () => ({})
      }
    },
    computed:{

    },
    watch:{
      properties: {
        handler: function(value) {
          this.visible = value.visible
          if (value.terminal) {
            this.terminal.ip = value.terminal.ip
            this.terminal.port = value.terminal.port
          }
        },
        immediate: true,
        deep: false
      }
    },
    created() {
      this.getRelayInfo();
    },
    data(){
      return {
        visible:false,
        title:'中继服务',
        loading:false,
        relayForm:{
          ip:"",
          stunPort:"",
          turnPort:"",
          username:"",
          password:"",
          enabled:"0"
        },
        rules: {
          ip: [{
            required: true,
            message: '请输入IP',
            trigger: 'blur'
          }],
          stunPort: [{
            required: true,
            message: '请输入端口',
            trigger: 'blur'
          }],
          turnPort: [{
            required: true,
            message: '请输入端口',
            trigger: 'blur'
          }],
          username: [{
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }],
          password: [{
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }]
        },
        terminal:{
          ip:'',
          port:''
        },
      }
    },
    methods:{
      getRelayInfo(){
        this.loading=true
        fetch(`http://${this.terminal.ip}:${this.terminal.port}/Renderers/Any/Relay/info`, {
            method: "POST",
            headers: { "Content-type": "application/json" },
        }).then(res => {
          if (!res.ok) { throw Error(res.statusText) }
          return res.json()
        }).then(json => {
          if(json.code==200 && json.result){
            this.relayForm.ip=json.result.ip || ''
            this.relayForm.stunPort=json.result.stunPort || ''
            this.relayForm.turnPort=json.result.turnPort || ''
            this.relayForm.username=json.result.username || ''
            this.relayForm.password=json.result.password || ''
            this.relayForm.enabled=json.result.enabled || '0'
          }else{
            this.relayForm.ip=''
            this.relayForm.stunPort=''
            this.relayForm.turnPort=''
            this.relayForm.username=''
            this.relayForm.password=''
            this.relayForm.enabled='0'
          }

        }).catch((error) => {
          this.$message({
            type: 'error',
            message:error
          })
        }).finally(() => {
          this.loading = false
        });
      },
      submitForm: function() {
        this.$refs.relayForm.validate((valid) => {
          if (valid) {
            this.loading=true
            fetch(`http://${this.terminal.ip}:${this.terminal.port}/Renderers/Any/Relay/update`, {
                method: "POST",
                headers: { "Content-type": "application/json" },
                body: JSON.stringify(
                {
                  ip:this.relayForm.ip,
                  stunPort:this.relayForm.stunPort,
                  turnPort:this.relayForm.turnPort,
                  username:this.relayForm.username,
                  password:this.relayForm.password,
                  enabled:this.relayForm.enabled
                })
            }).then(res => {
              if (!res.ok) { throw Error(res.statusText) }
              return res.json()
            }).then(json => {
              if(json.code==200){
                this.$message({
                  type: 'message',
                  message:json.message
                })
              }else{
                this.$message({
                  type: 'error',
                  message:json.message
                })
              }
            }).catch((error) => {
              this.$message({
                type: 'error',
                message:error
              })
            }).finally(() => {
              this.loading = false
            });

          }
        })
      },
      closed: function() {
        this.$emit('closed', false)
      },
    }
  }

</script>

<style>
</style>
